namespace Front._1127;

public partial class Login : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        using var connection = new MySqlConnection("server=localhost;port=3306;user id=root;password=你的数据库密码;database=你的数据库名;charset=utf8mb4;");
        connection.Open();

        using var command = new MySqlConnection("SELECT * FROM admin_info;", connection);
        using var reader = command.ExecuteReader();
        while (reader.Read())
            Console.WriteLine(reader.GetInt32(ordinal: 0));
    }
}


private void button1_Click(object sender, EventArgs e)
{

    using var connection =
        new MySqlConnection(
            "server=localhost;port=3306;user id=root;password=你的数据库密码;database=你的数据库名;charset=utf8mb4;");
    connection.Open();

    string sql = "SELECT * FROM admin_info where admin_account = @account and admin_pwd = @pwd";
    using var command = new MySqlCommand(sql, connection);

// 获取账号密码
    string account = this.textBox1.Text;
    string password = this.textBox2.Text;

// 对密码进行md5加密
    string pwd = MD5Helper.GetMD5(password);

// 给sql传参
    command.Parameters.AddWithValue("@account", account);
    command.Parameters.AddWithValue("@pwd", pwd);

    using var reader = command.ExecuteReader();
    if (reader.HasRows)
    {
        // 登录成功，跳转主窗体
        MainForm mainForm = new MainForm();
        mainForm.Show();
        // 隐藏登录窗体
        this.Hide();
    }
    else
    {
        // 登录失败
        MessageBox.Show(text: "账号或密码错误");
        // 清空输入框
        this.textBox1.Text = "";
        this.textBox2.Text = "";
    }
}